<template>
  <div class="log-box">
    <div class="log-title">{{ info.log }}</div>
    <div class="log-main">
      <div class="log-left">
        <div class="left-main">
          <je-input
            v-model:value="searchName"
            placeholder="请输入姓名"
            autocomplete="false"
            class="left-search-box"
          >
            <template #prefix>
              <i class="fal fa-search" style="padding-right: 2px"></i>
            </template>
          </je-input>
          <div class="user-box">
            <div v-show="accountList.length > 0">
              <div
                v-for="userItem in accountList"
                :key="userItem.accountId"
                class="user-item"
                :class="{
                  'normal-user-item': userItem.accountId != selectAccountId,
                  'select-user-item': userItem.accountId == selectAccountId,
                }"
                @click="handleAccount(userItem.accountId)"
              >
                <div v-if="userItem.fileKey && userItem.fileKey != ''" class="recore-avatar"
                  ><img :src="formatAvatar(userItem.fileKey)" alt="" />
                </div>
                <div
                  v-if="!userItem.fileKey || userItem.fileKey == ''"
                  class="user-card"
                  v-text="formatName(userItem.accountName)"
                ></div>
                <div class="user-nick">{{ userItem.accountName }}</div>
              </div>
            </div>
            <div v-show="accountList.length == 0" class="no-user-list"> 暂无数据 </div>
          </div>
        </div>
      </div>
      <div class="log-right">
        <div class="right-title">{{ info.logDetail }}</div>
        <div class="detail-main">
          <div v-show="userLogList.length > 0 && !loading" class="detail-box">
            <div
              v-for="logItem in userLogList"
              :key="logItem.values.JE_CORE_DEVELOPLOG_ID"
              class="detail-item"
            >
              <div
                v-if="logItem.values.fileKey && logItem.values.fileKey != ''"
                class="recore-avatar"
              >
                <img :src="formatAvatar(logItem.values.fileKey)" alt="" />
              </div>
              <div
                v-if="!logItem.values.fileKey || logItem.values.fileKey == ''"
                class="detail-card"
                v-text="formatName(logItem.values.DEVELOPLOG_USERNAME)"
              ></div>
              <div class="detail-name">{{ logItem.values.DEVELOPLOG_USERNAME }} {{}}</div>
              <div class="detail-date">{{ logItem.values.SY_CREATETIME }}</div>
              <div class="detail-info"
                >{{ logItem.values.DEVELOPLOG_ACT_NAME }}【{{
                  logItem.values.DEVELOPLOG_TYPE_NAME
                }}】{{ logItem.values.DEVELOPLOG_NAME }}</div
              >
            </div>
          </div>
          <div v-loading="loading"></div>
          <div v-show="userLogList.length > 0 && !loading" class="log-detail-page">
            <je-pagebar
              :page-size="pageData.limit"
              :total-count="pageData.totalCount"
              @load="getDevelopUserLogList"
            ></je-pagebar>
          </div>
          <div v-show="userLogList.length == 0 && !loading" class="no-data-box">
            <img :src="noData" alt="" />
            <div class="no-data-explain">暂无数据～</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { defineComponent, toRefs, onMounted, reactive, ref, watch } from 'vue';
  import JePagebar from '@/components/page-bar.vue';
  import { useI18n } from '@common/locales';
  import { Input } from '@jecloud/ui';
  import noData from '@/assets/no-data.png';
  import { getDevelopUser, getDevelopUserLog } from '@/api';
  import { getFileUrlByKey } from '@jecloud/utils';
  export default defineComponent({
    name: 'DevelopLog',
    components: {
      JeInput: Input,
      JePagebar,
    },
    setup() {
      const t = useI18n().t;
      let loading = ref(false); //请求数据loading
      let searchTimeout = null; //开发者搜索
      const state = reactive({
        info: {
          log: t('developBoard.log'),
          logDetail: t('developBoard.logDetail'),
        },
        noData,
        searchName: '', //开发者名称
        allAccountList: [], //所以开发者列表
        accountList: [], //开发者列表
        userLogList: [], //开发者日志列表
        pageData: {
          accountId: '', //用户id
          start: 1, //页码
          limit: 10, //限制条数
          totalCount: 0, //总条数
        },
        selectAccountId: '', //选中的accountId
      });
      const methods = {
        formatName(name) {
          if (name) {
            return name.slice(name.length - 2);
          }
        },
        getDevelopUserList() {
          //获取开发者信息
          getDevelopUser().then((res) => {
            if (res.code == 1000 && res.success) {
              state.allAccountList = [...res.data];
              state.accountList = res.data;
            } else {
              state.accountList = [];
            }
          });
        },
        getDevelopUserLogList(pageInfo) {
          if (pageInfo) {
            state.pageData.start = pageInfo.page;
          }
          //获取开发者日志信息
          loading.value = true;
          getDevelopUserLog(state.pageData).then((res) => {
            if (res.code == 1000 && res.success) {
              const resData = res.data;
              state.userLogList = resData.rows;
              state.pageData.totalCount = resData.totalCount;
              setTimeout(() => {
                loading.value = false;
              }, 100);
            } else {
              state.userLogList = [];
              loading.value = false;
            }
          });
        },
        handleAccount(accountId) {
          //操作选中的用户
          if (state.selectAccountId) {
            //有选中的用户id
            if (state.selectAccountId == accountId) {
              //选中的用户id和当前操作的id相等
              state.selectAccountId = '';
            } else {
              //选中的用户id和当前操作的id不相等
              state.selectAccountId = accountId;
            }
          } else {
            //没有选中的用户id
            state.selectAccountId = accountId;
          }
          state.pageData.accountId = state.selectAccountId;
          state.pageData.start = 1;
          methods.getDevelopUserLogList();
        },
        formatAvatar(fileKey) {
          //头像转换
          return getFileUrlByKey(fileKey);
        },
      };
      watch(
        () => state.searchName,
        (newVal, oldVal) => {
          if (newVal !== oldVal) {
            clearTimeout(searchTimeout);
            searchTimeout = setTimeout(() => {
              state.accountList = state.allAccountList.filter((item) => {
                return item.accountName.indexOf(newVal) != -1;
              });
            }, 1000);
          }
        },
      );
      onMounted(() => {
        methods.getDevelopUserList();
        methods.getDevelopUserLogList();
      });
      return {
        loading,
        ...toRefs(state),
        ...methods,
      };
    },
  });
</script>
<style>
  .left-search-box .ant-input {
    background: #ffffff;
  }
</style>
<style scoped lang="less">
  .log-box {
    width: 100%;
    margin-top: 20px;
    border-radius: 4px;
    background-color: #ffffff;
    box-sizing: border-box;
    .log-title {
      width: 100%;
      height: 60px;
      line-height: 60px;
      padding: 0 20px;
      font-size: 16px;
      font-weight: bold;
      color: #3f3f3f;
    }
    .log-main {
      width: 100%;
      height: 518px;
      display: flex;
      .log-left {
        width: 240px;
        height: 100%;
        box-sizing: border-box;
        .left-main {
          width: 200px;
          margin: 0 auto;
          .left-search-box {
            width: 100%;
            height: 32px;
            border-radius: 2px;
            background: #ffffff;
            font-size: 14px;
            color: #8d98ab;
          }
          .user-box {
            width: 100%;
            height: 476px;
            overflow-y: auto;
            .user-item {
              width: 100%;
              height: 44px;
              display: flex;
              align-items: center;
              padding-left: 20px;
              padding-top: 10px;
              .user-card {
                width: 34px;
                height: 34px;
                line-height: 34px;
                border-radius: 50%;
                background-color: #efa544;
                color: #ffffff;
                font-size: 12px;
                text-align: center;
              }
              .user-nick {
                width: 116px;
                height: 20px;
                line-height: 20px;
                font-size: 14px;
                color: #3f3f3f;
                padding-left: 10px;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                cursor: pointer;
              }
            }
          }
          .user-box::-webkit-scrollbar {
            width: 0;
            height: 0;
          }
        }
      }
      .log-right {
        width: 1010px;
        padding-right: 20px;
        .right-title {
          width: 100%;
          height: 32px;
          line-height: 32px;
          padding-left: 10px;
          font-size: 14px;
          color: #3f3f3f;
          box-sizing: border-box;
          background-color: #f8faff;
        }
        .detail-main {
          width: 100%;
          height: 476px;
          border-top: 1px solid #f8faff;
          border-left: 2px solid #f8faff;
          box-sizing: border-box;
          position: relative;
        }
        .detail-box {
          width: 100%;
          height: 440px;
          padding-left: 10px;
          overflow-y: auto;
          box-sizing: border-box;
          .detail-item {
            width: 100%;
            height: 44px;
            display: flex;
            align-items: center;
            padding-top: 10px;
            .detail-card {
              width: 34px;
              height: 34px;
              line-height: 34px;
              border-radius: 50%;
              background-color: #efa544;
              color: #ffffff;
              font-size: 12px;
              text-align: center;
            }
            .detail-name {
              width: 90px;
              padding: 0 10px;
            }
            .detail-date {
              width: 197px;
              padding-left: 10px;
            }
            .detail-name,
            .detail-date,
            .detail-info {
              height: 20px;
              line-height: 20px;
              font-size: 14px;
              color: #3f3f3f;
              padding-left: 10px;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
            }
          }
        }
        .detail-box::-webkit-scrollbar {
          width: 0;
          height: 0;
        }
        .log-detail-page {
          width: 100%;
          height: 36px;
          box-sizing: border-box;
          padding: 10px 0 0px 10px;
        }
      }
    }
    .no-data-box {
      width: 100%;
      padding-top: 130px;
      img {
        display: block;
        width: 319px;
        height: 194px;
        margin: 0 auto;
      }
      .no-data-explain {
        width: 100%;
        text-align: center;
        font-size: 14px;
        color: #a9aba9;
        padding-top: 20px;
      }
    }
    .recore-avatar {
      width: 34px;
      height: 34px;
      text-align: center;
      border-radius: 100%;
      img {
        width: 34px;
        height: 34px;
        border-radius: 100%;
      }
    }
    .no-user-list {
      width: 100%;
      height: 476px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #8d98ab;
      font-size: 14px;
    }
  }
</style>
